/**
 * RtmpView.js
 * Created by Eric Silverberg on 2018/04/07.
 * Updated by Rockvic on 2020/05/07.
 * Copyright © 2018 Perry Street Software. All rights reserved.
*/

import React, { Component } from "react";
import { PropTypes } from "prop-types";
import { requireNativeComponent, View, UIManager, findNodeHandle } from "react-native";

const RCT_VIDEO_REF = "RtmpView";

class RtmpView extends Component {
  constructor(props) {
    super(props);
  }

  componentDidMount() {
    this.initialize();
  }

  _onPlaybackState = (event) => {
    if (!this.props.onPlaybackState) {
      return;
    }
    this.props.onPlaybackState(event.nativeEvent)
  }

  _onLoadState = (event) => {
    if (!this.props.onLoadState) {
      return;
    }
    this.props.onLoadState(event.nativeEvent)
  }

  _onFirstVideoFrameRendered = (event) => {
    if (!this.props.onFirstVideoFrameRendered) {
      return;
    }
    this.props.onFirstVideoFrameRendered(event.nativeEvent)
  }

  _onBitrateRecalculated = (event) => {
    if (!this.props.onBitrateRecalculated) {
      return;
    }
    this.props.onBitrateRecalculated(event.nativeEvent)
  }

  initialize() {
    UIManager.dispatchViewManagerCommand(
      findNodeHandle(this.refs[RCT_VIDEO_REF]),
      UIManager.getViewManagerConfig("RNRtmpView").Commands.initialize,
      null
    );
  }

  pause() {
    UIManager.dispatchViewManagerCommand(
      findNodeHandle(this.refs[RCT_VIDEO_REF]),
      UIManager.getViewManagerConfig("RNRtmpView").Commands.pause,
      null
    );
  }

  play() {
    UIManager.dispatchViewManagerCommand(
      findNodeHandle(this.refs[RCT_VIDEO_REF]),
      UIManager.getViewManagerConfig("RNRtmpView").Commands.play,
      null
    );
  }

  stop() {
    UIManager.dispatchViewManagerCommand(
      findNodeHandle(this.refs[RCT_VIDEO_REF]),
      UIManager.getViewManagerConfig("RNRtmpView").Commands.stop,
      null
    );
  }

  mute() {
    UIManager.dispatchViewManagerCommand(
      findNodeHandle(this.refs[RCT_VIDEO_REF]),
      UIManager.getViewManagerConfig("RNRtmpView").Commands.mute,
      null
    );
  }

  unmute() {
    UIManager.dispatchViewManagerCommand(
      findNodeHandle(this.refs[RCT_VIDEO_REF]),
      UIManager.getViewManagerConfig("RNRtmpView").Commands.unmute,
      null
    );
  }

  render() {
    return (
      <RNRtmpView
        ref={RCT_VIDEO_REF}
        // scalingMode={this.props.scalingMode} // "MovieScalingModeAspectFill"
        // resizeMode={this.props.resizeMode}
        shouldMute={false}
        playOnResume
        pauseOnStop
        onPlaybackState={this._onPlaybackState.bind(this)}
        onLoadState={this._onLoadState.bind(this)}
        onFirstVideoFrameRendered={this._onFirstVideoFrameRendered.bind(this)}
        onBitrateRecalculated={this._onBitrateRecalculated.bind(this)}
        {...this.props}
      />
    );
  }
}

RtmpView.RESIZE_MODE = {
  RESIZE_MODE_FIT: 0,
  RESIZE_MODE_FIXED_WIDTH: 1,
  RESIZE_MODE_FIXED_HEIGHT: 2,
  RESIZE_MODE_FILL: 3,
  RESIZE_MODE_ZOOM: 4
};

RtmpView.SCALING_MODE = {
  VIDEO_SCALING_MODE_SCALE_TO_FIT: 1,
  VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING: 2
};

RtmpView.name = RCT_VIDEO_REF;
RtmpView.propTypes = {
  url: PropTypes.string,
  shouldMute: PropTypes.bool,
  playOnResume: PropTypes.bool,
  pauseOnStop: PropTypes.bool,
  onPlaybackState: PropTypes.func,
  onLoadState: PropTypes.func,
  onFirstVideoFrameRendered: PropTypes.func,
  onBitrateRecalculated: PropTypes.func,
  resizeMode: PropTypes.number,
  scalingMode: PropTypes.number,
  ...View.propTypes
};
RtmpView.defaultProps = {
  resizeMode: RtmpView.RESIZE_MODE.RESIZE_MODE_FILL,
  scalingMode: RtmpView.SCALING_MODE.VIDEO_SCALING_MODE_SCALE_TO_FIT
}

const RNRtmpView = requireNativeComponent("RNRtmpView", RtmpView, {
});

module.exports = RtmpView;
